package com.jackchen.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.log4j.Logger;

import com.jackchen.common.transform.TransformException;
import com.jackchen.common.transform.Transformer;
import com.jackchen.dao.CategoryDAO;
import com.jackchen.dao.UserDAO;
import com.jackchen.dao.UserGroupDAO;
import com.jackchen.po.User;
import com.jackchen.po.Usergroup;
import com.jackchen.service.UserGroupService;
import com.jackchen.vo.UserGroupVO;

public class UserGroupServiceImpl extends IBaseServiceImpl implements
		UserGroupService {
	private static final Logger log = Logger.getLogger(UserGroupServiceImpl.class);
	private UserGroupDAO userGroupDAO;
	private UserDAO userDAO;
	private CategoryDAO categoryDAO;

	@Override
	public List<UserGroupVO> queryAllUserGroup() {
		List dataList = userGroupDAO.findAll("UserGroup");
		try {
			return Transformer.pojoToVos(dataList, UserGroupVO.class);
		} catch (TransformException e) {
			log.error(e.getMessage());
		}
		return Collections.emptyList();
	}
	
	@Override
	public Map queryUserGroupByUserOnPage(String userName, int pageSize,
			int startRow) {
		User user = userDAO.findByUserName(userName);
		if (user == null) {
			return Collections.emptyMap();
		}
		
		try {
			// 只有admin用户的isFirst字段为1，普通用户都为0
			if (user.getIsAdmin()) {
				Map groupMap = userGroupDAO.findByPage("UserGroup", pageSize, startRow);
				List<Usergroup> dataList= (List<Usergroup>) groupMap.get("querylist");
				
				List<UserGroupVO> regionGroups = Transformer.pojoToVos((List)dataList, UserGroupVO.class);
				groupMap.put("querylist", regionGroups);
				return groupMap;
			}
		} catch (TransformException e) {
			log.error(e.getMessage());
		}
		return Collections.emptyMap();
	}
	
	@Override
	public Object[] deleteUserGroups(String[] ids) {
		if (ids == null || ids.length == 0) {
			return null;
		}
		
		// 检查这些用户组下面是否还挂载有用户，如果还有用户则提示不允许删除
		List<String> repeatGroups = new ArrayList<String>();
		List<Usergroup> userGroups = userGroupDAO.findUserGroupByIds(ids);
		for (Usergroup userGroup : userGroups) {
			Set users = userGroup.getUsers();
			if (users != null && !users.isEmpty()) {
				repeatGroups.add(userGroup.getGroupname());
			}
		}
		// 有不允许删除的用户组
		if (!repeatGroups.isEmpty()) {
			return new Object[]{"repeat", repeatGroups};
		}
		
		// 通过检查后删除用户组
		int result = userGroupDAO.deleteUserGroups(ids);
		if (result == ids.length) {
			return new Object[]{"success"};
		} else {
			return new Object[]{"db", result};
		}
	}
	
	@Override
	public int[] queryUserGroupLevelRange(String userName) {
		User user = userDAO.findByUserName(userName);
		if (user == null) {
			return null;
		}
		
		// 获得当前登陆用户的Group Level
		Usergroup group = user.getUsergroup();
		int groupLevel = group.getGrouplevel();
		
		// 查询最大MenuGrade
//		int maxMenuGrade = categoryDAO.findMaxMenuGrade();
		
//		return new int[]{groupLevel, maxMenuGrade};
		return new int[]{groupLevel};
	}
	
	
	
	@Override
	public boolean createNewUserGroup(String name, String desc, int level) {
		Usergroup group = new Usergroup();
		group.setGrouplevel(level);
		group.setGroupdesc(desc);
		group.setGroupname(name);
		userGroupDAO.save(group);		
		return true;
	}

	@Override
	public UserGroupVO queryEditUserGroup(String userGroupId) {
		Usergroup userGroup = userGroupDAO.findById(Usergroup.class, userGroupId);
		if (userGroup == null) {
			return null;
		}
		try {
			return Transformer.pojoToVo(userGroup, UserGroupVO.class);
		} catch (TransformException e) {
			log.error(e.getMessage());
		}
		return null;
	}
	
	@Override
	public boolean updateUserGroup(String id, String name, String desc, int level) {
		Usergroup group = userGroupDAO.findById(Usergroup.class, id);
		if (group == null) {
			return false;
		}
		
		group.setGrouplevel(level);
		group.setGroupdesc(desc);
		group.setGroupname(name);
		
		userGroupDAO.update(group);
		return true;
	}

	public UserGroupDAO getUserGroupDAO() {
		return userGroupDAO;
	}

	public void setUserGroupDAO(UserGroupDAO userGroupDAO) {
		this.userGroupDAO = userGroupDAO;
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public CategoryDAO getCategoryDAO() {
		return categoryDAO;
	}

	public void setCategoryDAO(CategoryDAO categoryDAO) {
		this.categoryDAO = categoryDAO;
	}

}
